<template>
  <div class="cate">
    <van-nav-bar class="header" title="分类" />
    <div class="content">
      <div class="aside">
        <van-sidebar v-model="active" @change="onChange" v-if="cates">
          <van-sidebar-item
            v-for="cate in cates"
            :key="cate.id"
            :title="cate.name"
          />
        </van-sidebar>
      </div>
      <div class="lists">
        <div class="items" v-if="cates[current] && cates[current].child">
          <div
            class="item"
            v-for="item in cates[current].child"
            :key="item.id"
            @click="$router.push('/goodsList/' + item.id)"
          >
            <div class="img"><img :src="item.image_url" /></div>
            <p class="item-name">{{ item.name }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getHome } from "@/api/home";
import { ref } from "vue";
import { Toast } from "vant";

export default {
  name: "cate",
  data() {
    return {
      cates: [],
      current: 0,
    };
  },
  setup() {
    const active = ref(0);
    const onChange = (index) => Toast(`标签名 ${index + 1}`);
    return {
      active,
      onChange,
    };
  },
  created() {
    getHome(
      { method: "categories.getallcat" },
      {
        loading: {
          message: "加载中...",
          forbidClick: true,
          loadingType: "spinner",
        },
      }
    ).then((res) => (this.cates = res.data.data));
  },
};
</script>

<style scoped>
.cate .content {
  display: flex;
  position: absolute;
  top: 0.92rem;
  left: 0;
  right: 0;
  bottom: 0;
}
.cate .content .aside {
  width: 1.6rem;
}
.cate .content .lists {
  flex: 1;
  overflow-y: auto;
}
.van-sidebar-item {
  font-size: 0.24rem;
}
.van-sidebar-item--select:before {
  background-color: #333;
  height: 0.56rem;
}
.lists .items {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}
.lists .items .item {
  width: 33.33%;
  padding: 0.1rem;
  text-align: center;
}
.lists .items .item .img {
  position: relative;
  width: 1.7rem;
  height: 1.7rem;
  overflow: hidden;
}
.lists .items .item .img img {
  width: 100%;
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
}
.item-name {
  color: #666;
  width: 1.7rem;

  font-size: 0.26rem;
  line-height: 0.5rem;
}
</style>